using System.ComponentModel.DataAnnotations;
using OneCaps.Core.Enum;

namespace OneCaps.Core.Entity
{
    /// <summary>
    /// 车位实体
    /// </summary>
    public class ParkingSpace : BaseEntity<int>
    {
        /// <summary>
        /// 车位编号
        /// </summary>
        [Required]
        [MaxLength(20)]
        public string SpaceNumber { get; set; } = string.Empty;

        /// <summary>
        /// 车位类型
        /// </summary>
        public ParkingSpaceType Type { get; set; } = ParkingSpaceType.Standard;

        /// <summary>
        /// 车位状态
        /// </summary>
        public ParkingSpaceStatus Status { get; set; } = ParkingSpaceStatus.Available;

        /// <summary>
        /// 所属区域
        /// </summary>
        [MaxLength(50)]
        public string Area { get; set; } = string.Empty;

        /// <summary>
        /// 楼层
        /// </summary>
        public int Floor { get; set; } = 1;

        /// <summary>
        /// 行号
        /// </summary>
        public int Row { get; set; } = 1;

        /// <summary>
        /// 列号
        /// </summary>
        public int Column { get; set; } = 1;

        /// <summary>
        /// 是否充电桩车位
        /// </summary>
        public bool HasChargingStation { get; set; } = false;

        /// <summary>
        /// 是否无障碍车位
        /// </summary>
        public bool IsAccessible { get; set; } = false;

        /// <summary>
        /// 是否VIP车位
        /// </summary>
        public bool IsVipSpace { get; set; } = false;

        /// <summary>
        /// 车位尺寸（长x宽）
        /// </summary>
        [MaxLength(20)]
        public string? Dimensions { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        [MaxLength(200)]
        public string? Remarks { get; set; }

        /// <summary>
        /// 当前停车记录
        /// </summary>
        public virtual ParkingRecord? CurrentParkingRecord { get; set; }

        /// <summary>
        /// 停车记录历史
        /// </summary>
        public virtual ICollection<ParkingRecord> ParkingRecords { get; set; } = new List<ParkingRecord>();

        // 领域方法
        /// <summary>
        /// 检查车位是否可用
        /// </summary>
        public bool IsAvailable()
        {
            return Status == ParkingSpaceStatus.Available && CurrentParkingRecord == null;
        }

        /// <summary>
        /// 占用车位
        /// </summary>
        public void Occupy(ParkingRecord record)
        {
            if (!IsAvailable())
            {
                throw new InvalidOperationException($"车位 {SpaceNumber} 不可用");
            }

            Status = ParkingSpaceStatus.Occupied;
            CurrentParkingRecord = record;
            UpdateTime = DateTime.Now;
        }

        /// <summary>
        /// 释放车位
        /// </summary>
        public void Release()
        {
            if (Status != ParkingSpaceStatus.Occupied)
            {
                throw new InvalidOperationException($"车位 {SpaceNumber} 未被占用");
            }

            Status = ParkingSpaceStatus.Available;
            CurrentParkingRecord = null;
            UpdateTime = DateTime.Now;
        }

        /// <summary>
        /// 维护车位
        /// </summary>
        public void SetMaintenance()
        {
            if (Status == ParkingSpaceStatus.Occupied)
            {
                throw new InvalidOperationException($"车位 {SpaceNumber} 正在使用中，无法设置维护");
            }

            Status = ParkingSpaceStatus.Maintenance;
            UpdateTime = DateTime.Now;
        }

        /// <summary>
        /// 获取车位位置描述
        /// </summary>
        public string GetLocationDescription()
        {
            return $"{Area} {Floor}层 {Row}行{Column}列";
        }

        /// <summary>
        /// 检查是否适合指定车辆类型
        /// </summary>
        public bool IsSuitableForVehicle(VehicleType vehicleType)
        {
            return vehicleType switch
            {
                VehicleType.Motorcycle or VehicleType.Electric => Type == ParkingSpaceType.Motorcycle,
                VehicleType.Truck or VehicleType.Bus => Type == ParkingSpaceType.Large,
                _ => Type == ParkingSpaceType.Standard || Type == ParkingSpaceType.Large
            };
        }
    }
} 